﻿using Microsoft.AspNetCore.Mvc;
using Qy.Common;
using Qy.Models;
using Qy.Service;
using Qy.Web.Authorize;
using System.Threading.Tasks;

namespace Qy.Web.Api;

[Route("Api/[controller]/[action]")]
[Verify]
public class ConfigController : Base
{
    private readonly ConfigService _configService;
    public ConfigController(ConfigService configService)
    {
        _configService = configService;
    }

    [HttpGet]
    [ActionPermissionFilter(Permission = PermissionKeys.Config_Index)]
    public async Task<IActionResult> Index()
    {
        return Json(new { status = 1, data= await _configService.GetSytemConfigAsync() });
    }


    [HttpPut]
    [XssFieldFilter]//防Xss
    [ActionPermissionFilter(Permission = PermissionKeys.Config_Edit)]
    [Log(Name = "编辑系统配置")]
    public async Task<IActionResult> EditAsync([FromBody] SytemConfig data)
    {
        if (ModelState.IsValid)
        {
            int res = await _configService.UpdateByNameAsync(data);
            return res > 0 ? Json(new { status = 1, msg = "修改系统配置 " + res + " 项" }) : Json(new { status = 0, msg = "修改系统配置 " });
        }
        else
        {
            var error = ModelState.GetErrorMessages();
            return Json(new { status = 0, msg = "值不能为空：" + error });
        }

    }
}
